from flask import Flask
import redis
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CsrfProtect
from flask_session import Session
from config import config

db = SQLAlchemy()
redis_store = None


def create_app(config_name):
    global redis_store
    app = Flask(__name__)
    app.config.from_object(config[config_name])

    # 配置redis
    redis_store = redis.StrictRedis(host=config[config_name].REDIS_HOST, port=config[config_name].REDIS_PORT)
    CsrfProtect(app)  # 使用表单里面的csrf跨站请求保护功能
    Session(app)      # 设置session保存位置

    return app
